package com.dingding.common.core.util.prop;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

/**
 * 
 * @ClassName: Prop 
 * @Description:properties文件读取类
 * @author CHEHAUNBO 
 * @date 2016年5月25日 下午8:22:33
 * 
 */
public class Prop {

	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 多个properties对象集合
	 */
	private static final ConcurrentHashMap<String, Prop> map = new ConcurrentHashMap<String, Prop>();

	private Properties properties = null;

	public Prop(String fileName) {
		this(fileName, "UTF-8");
	}

	public Prop(String fileName, String encoding) {

		this.getProperties(fileName, encoding);

	}

	public static Prop use(String fileName) {
		return use(fileName, "UTF-8");
	}

	public static Prop use(String fileName, String encoding) {
		Prop result = map.get(fileName);
		if (result == null) {
			result = new Prop(fileName, encoding);
			map.put(fileName, result);
		}
		return result;
	}

	/**
	 * 
	 * 获取Properties文件
	 * 
	 * @Title: getProperties 
	 * @param fileName 文件名称
	 * @param encoding 编码格式
	 * @author CHEHAUNBO
	 * 
	 */
	private void getProperties(String fileName, String encoding) {

		InputStream inputStream = null;
		try {
			inputStream = Prop.class.getResourceAsStream(fileName);
			if (inputStream == null) {
				throw new IllegalArgumentException("要读取的properties文件不存在！" + fileName);
			}
			properties = new Properties();
			properties.load(new InputStreamReader(inputStream, encoding));
		} catch (IOException e) {
			throw new RuntimeException("读取properties出现异常！", e);
		} finally {
			if (inputStream != null)
				try {
					inputStream.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
		}

	}

	/**
	 * 
	 * 获取properties中key的值
	 * 
	 * @param key 值的key
	 * @author CHEHAUNBO
	 * 
	 */
	public String get(String key) {
		return properties.getProperty(key);
	}

	public Set<String> getKeySet() {

		Set<String> keyValue = new HashSet<String>();
		for (Iterator<Object> it = properties.keySet().iterator(); it.hasNext();) {
			String key = (String) it.next();
			keyValue.add(key);
		}

		return keyValue;

	}

	/**
	 * 
	 * 获取properties中key的值,如果不存在当前key则返回默认值
	 * 
	 * @Title: get 
	 * @param key 值的key
	 * @param defaultValue 未获取到值时返回的默认值
	 * @author CHEHAUNBO
	 * 
	 */
	public String get(String key, String defaultValue) {
		return properties.getProperty(key, defaultValue);
	}

	/**
	 * 
	 * 获取properties中key的值
	 * 
	 * @param key 值的key
	 * @author CHEHAUNBO
	 * 
	 */
	public Integer getInt(String key) {
		return getInt(key, null);
	}

	/**
	 * 
	 * 获取properties中key的值,如果不存在当前key则返回默认值
	 * 
	 * @Title: get 
	 * @param key 值的key
	 * @param defaultValue 未获取到值时返回的默认值
	 * @author CHEHAUNBO
	 * 
	 */
	public Integer getInt(String key, Integer defaultValue) {
		String value = properties.getProperty(key);
		if (value != null)
			return Integer.parseInt(value.trim());
		return defaultValue;
	}

	/**
	 * 
	 * 获取properties中key的值
	 * 
	 * @param key 值的key
	 * @author CHEHAUNBO
	 * 
	 */
	public Long getLong(String key) {
		return getLong(key, null);
	}

	/**
	 * 
	 * 获取properties中key的值
	 * 
	 * @param key 值的key
	 * @author CHEHAUNBO
	 * 
	 */
	public Double getDouble(String key) {
		return getDouble(key, null);
	}

	/**
	 * 
	 * 获取properties中key的值,如果不存在当前key则返回默认值
	 * 
	 * @Title: getLong 
	 * @param key 值的key
	 * @param defaultValue 未获取到值时返回的默认值
	 * @author CHEHAUNBO
	 * 
	 */
	public Long getLong(String key, Long defaultValue) {
		String value = properties.getProperty(key);
		if (value != null)
			return Long.parseLong(value.trim());
		return defaultValue;
	}

	/**
	 * 
	 * 获取properties中key的值,如果不存在当前key则返回默认值
	 * 
	 * @Title: getDouble 
	 * @param key 值的key
	 * @param defaultValue 未获取到值时返回的默认值
	 * @author CHEHAUNBO
	 * 
	 */
	public Double getDouble(String key, Double defaultValue) {
		String value = properties.getProperty(key);
		if (value != null)
			return Double.parseDouble(value.trim());
		return defaultValue;
	}

	/**
	 * 
	 * 获取properties中key的值
	 * 
	 * @param key 值的key
	 * @author CHEHAUNBO
	 * 
	 */
	public Boolean getBoolean(String key) {
		return getBoolean(key, null);
	}

	/**
	 * 
	 * 获取properties中key的值,如果不存在当前key则返回默认值
	 * 
	 * @Title: get 
	 * @param key 值的key
	 * @param defaultValue 未获取到值时返回的默认值
	 * @author CHEHAUNBO
	 * 
	 */
	public Boolean getBoolean(String key, Boolean defaultValue) {
		String value = properties.getProperty(key);
		if (value != null) {
			value = value.toLowerCase().trim();
			if ("true".equals(value))
				return true;
			else if ("false".equals(value))
				return false;
			throw new RuntimeException("不能将properties中key的值转换成boolean值！");
		}
		return defaultValue;
	}

	/**
	 * 
	 * 判断properties文件中是否存在要获取的key
	 * @Title: containsKey 
	 * @param key 验证key
	 * @return  boolean true 存在 false 不存在 
	 * @author CHEHAUNBO
	 * 
	 */
	public boolean containsKey(String key) {
		return properties.containsKey(key);
	}

	public Properties getProperties() {
		return properties;
	}
}
